<template>
  <el-dialog
    :model-value="modelValue"
    @update:model-value="updateVisible"
    :close-on-click-modal="false"
    :title="button"
  >
    <el-form
      :model="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="120px"
    >
      <el-form-item label="sku号" prop="skuId">
        <el-input v-model="dataForm.skuId" placeholder="sku号"></el-input>
      </el-form-item>
      <el-form-item label="仓库" prop="wareId">
        <el-select v-model="dataForm.wareId" placeholder="请选择仓库" clearable>
          <el-option
            v-for="w in wareList"
            :label="w.name"
            :value="w.id"
            :key="w.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="库存数" prop="stock">
        <el-input v-model="dataForm.stock" placeholder="库存数"></el-input>
      </el-form-item>
      <el-form-item label="sku名称" prop="skuName">
        <el-input v-model="dataForm.skuName" placeholder="sku名称"></el-input>
      </el-form-item>
      <el-form-item label="锁定库存" prop="stockLocked">
        <el-input
          v-model="dataForm.stockLocked"
          placeholder="锁定库存"
        ></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleCancel">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit()">
          {{ button }}
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup lang="ts">
import { nextTick, ref, defineExpose, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { reqWareInfoList } from '@/api/ware/wareinfo'
import { reqWareSkuInfo, reqWareSkuUpdateCommit } from '@/api/ware/sku'

const dataForm = ref({
  id: 0,
  skuId: '',
  wareId: '',
  stock: 0,
  skuName: '',
  stockLocked: 0,
})
const button = ref('添加')
const wareList = ref([
  {
    id: 0,
    name: '',
  },
])

defineProps(['modelValue'])

const emit = defineEmits(['update:modelValue'])

const updateVisible = (value: boolean) => {
  emit('update:modelValue', value)
}

// 处理取消按钮点击
const handleCancel = () => {
  updateVisible(false)
}

const getWares = async () => {
  try {
    const response = await reqWareInfoList(1, 500, '')
    if (response && response.code === 0) {
      wareList.value = response.page.list
    }
  } catch (error) {
    console.error(error)
  }
}

const dataFormSubmit = async () => {
  try {
    await reqWareSkuUpdateCommit(
      dataForm.value.id > 0 ? 'update' : 'save',
      dataForm.value.id,
      dataForm.value.skuId,
      dataForm.value.wareId,
      dataForm.value.stock,
      dataForm.value.skuName,
      dataForm.value.stockLocked,
    )
    ElMessage.success(`库存${button.value}成功`)
  } catch (error) {
    console.error(`库存${button.value}失败`, error)
  }
}

const init = (id: number) => {
  button.value = id > 0 ? '修改' : '添加'
  nextTick(async () => {
    dataForm.value.id = 0
    dataForm.value.skuId = ''
    dataForm.value.wareId = ''
    dataForm.value.stock = 0
    dataForm.value.skuName = ''
    dataForm.value.stockLocked = 0
    if (id > 0) {
      try {
        const response = await reqWareSkuInfo(id)
        if (response.code === 0) {
          dataForm.value.id = response.wareSku.id
          dataForm.value.skuId = response.wareSku.skuId
          dataForm.value.wareId = response.wareSku.wareId
          dataForm.value.stock = response.wareSku.stock
          dataForm.value.skuName = response.wareSku.skuName
          dataForm.value.stockLocked = response.wareSku.stockLocked
        }
      } catch (error) {
        console.error('库存信息获取失败:', error)
      }
    }
  })
}

// 暴露方法
defineExpose({ init })

onMounted(() => {
  getWares()
})
</script>

<style scoped lang="scss"></style>
